<template>
  <div id="home">
    <div class="login-box">
      <form @submit.prevent="login">
        <h2>购物商场登录</h2>
        <div class="input-group">
          <input
            type="text"
            v-model="username"
            placeholder="请输入用户名"
            id="username"
            required
          />
        </div>
        <div class="input-group">
          <input
            type="password"
            v-model="password"
            placeholder="请输入密码"
            id="password"
            required
          />
        </div>
        <div class="input-group">
          <button type="submit" class="login-button">登录</button>
        </div>
        <div class="register" @click="register">立即注册</div>
        <br />
      </form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      username: "",
      password: "",
      User: "",
    };
  },
  methods: {
    login() {
      let data = {
        username: this.username,
        password: this.password,
      };
      // 设置axios请求的headers，指定内容类型为application/json
      const config = {
        headers: {
          "Content-Type": "application/json",
        },
      };
      axios
        .post("http://localhost:8080/users/login", JSON.stringify(data), config)
        .then((response) => {
          console.log(response.data);
          sessionStorage.setItem("User", JSON.stringify(response.data));
          this.User = JSON.parse(sessionStorage.getItem("User"));
          this.$router.push({ name: "ProList" });
        })
        .catch((error) => {
          console.error("请求错误:", error);
          alert(error.response.data.message);
        });
    },
    register(){
      this.$router.push({ name: "Register" });
    }
  },
};
</script>


<style scoped>
#home {
  border: rgb(255, 255, 255) solid 1px;
  height: 1000px;
  background-image: url("/public/image/登录页面背景.jpg");
  background-size: 100% 100%;
}

.login-box {
  opacity: 0.75;
  width: 420px;
  height: 320px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  position: relative;
  margin-top: 13%;
  margin-left: 65%;
}

.login-box h2 {
  text-align: center;
  margin-bottom: 20px;
}

.input-group {
  margin-bottom: 15px;
  display: flex;
  flex-direction: column;
}

.input-group label {
  margin-bottom: 5px;
}

.input-group input[type="text"],
.input-group input[type="password"] {
  height: 30px;
  margin-bottom: 20px;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.input-group button[type="submit"] {
  padding: 10px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.input-group button[type="submit"]:hover {
  background-color: #0056b3;
}

.back-link {
  position: absolute;
  bottom: 10px;
  right: 10px;
}

.abutton {
  opacity: 0.75;
  padding: 10px 20px;
  font-size: 16px;
  color: #333;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s, color 0.3s;
}

.abutton:hover {
  background-color: #e9e9e9;
  color: #000;
}

.abutton:active {
  background-color: #d4d4d4;
  color: #000;
}

.login-button {
  padding: 12px 25px;
  font-size: 16px;
  color: #ffffff;
  background: linear-gradient(145deg, #6c63ff, #9c27b0);
  border: none;
  border-radius: 24px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: all 0.3s ease;
  outline: none;
}

.login-button:hover {
  background: linear-gradient(145deg, #6c63ff, #8e2de2);
  transform: translateY(-2px);
}

.login-button:active {
  background: linear-gradient(145deg, #5e50ce, #7b1fa2);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  transform: translateY(0);
}

.login-button:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(108, 99, 255, 0.5);
}
.register{
  margin-top: 2px;
  float:right;
}
.register:hover{
  color:aqua;
}
</style>
  